package com.test.clyq.daoImpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;

import com.test.clyq.dao.CategoryImgDao;
import com.test.clyq.entity.CategoryImg;
import com.test.clyq.entity.Users;
import com.test.dao.hibernate.BaseDaoHibernate;
import com.test.util.FormatExceptionStack;
import com.test.util.Pagination;
import com.test.util.SessionFactoryUtil;

/**
 * CategoryImgDaoHibernate. <br>
 * 栏目域图片管理.
 * <p>
 * Copyright: Copyright (c) 2010-4-17 下午04:36:39
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: liuxl@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
@SuppressWarnings("unchecked")
public class CategoryImgDaoHibernate extends BaseDaoHibernate implements CategoryImgDao {

	/**
	 * 根据栏目ID删除栏目
	 * 
	 * @param id
	 *            栏目ID
	 * @param user
	 *            用户实例
	 * @return true or false
	 */
	public boolean delCategoryImgById(Long id, Users user) {
		this.log.info("根据栏目ID删除栏目start");
		try {
			CategoryImg img = (CategoryImg) getItem(CategoryImg.class, id);
			if (img != null) {
				removeItem(CategoryImg.class, id, user);
				this.log.debug("删除栏目域图片成功");
				this.log.info("根据栏目ID删除栏目end");
				return true;
			} else {
				this.log.debug("删除栏目域图片失败");
				return false;
			}
		}
		catch (DataAccessException e) {
			this.log.error("[CategoryImg]delCategoryImgById发生错误！\n" + FormatExceptionStack.formatException(e));
			return false;
		}
	}

	/**
	 * 根据栏目id获得栏目域图片
	 * 
	 * @param id
	 *            图片ID
	 */
	public CategoryImg getCategoryImgById(Long id) {
		this.log.info("根据栏目ID获得栏目域图片start");
		try {
			CategoryImg img = (CategoryImg) getItem(CategoryImg.class, id);
			if (img != null) {
				this.log.info("根据栏目ID获得栏目域图片end");
				return img;
			} else {
				this.log.info("根据栏目ID获得栏目域图片end");
				return null;
			}

		}
		catch (DataAccessException e) {
			this.log.error("[CategoryImg]getCategoryImgById发生错误！\n" + FormatExceptionStack.formatException(e));
			return null;
		}
	}

	public List<CategoryImg> getCategoryImgsByCategoryId(Long categoryId, Pagination pagination) {
		SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
		Session session = sessionFactory.openSession();
		String hql = "From CategoryImg where categoryId = " + categoryId + " order by isPublic desc,orderId desc, categoryImgCreatetime desc";
		try {
			Query query = session.createQuery("select count(*) " + hql);
			List listCount = query.list();
			if (listCount != null && !listCount.isEmpty()) {
				pagination.setTotalRows(((Integer) listCount.get(0)).intValue()); // 获得记录总数
			} else {
				pagination.setTotalRows(0);
			}

			Query query1 = session.createQuery(hql.toString());
			query1.setFirstResult(pagination.getStartIndex());
			query1.setMaxResults(pagination.getNumPerPage());
			List list = query1.list();
			if (list != null && list.size() > 0) {
				this.log.debug("获得列表数据");
				this.log.info("获得栏目信息列表end");
				return list;
			} else {
				this.log.debug("表中没有数据");
				return null;
			}
		}
		catch (Exception e) {
			this.log.error("[CategoryImg]getCategoryImgsByCategoryId发生错误！\n" + FormatExceptionStack.formatException(e));
			return new ArrayList<CategoryImg>();
		}
		finally {
			SessionFactoryUtil.isClose(session);
		}
	}

	/**
	 * 保存或更新栏目域图片
	 * 
	 * @param img
	 *            图片实例
	 * @param user
	 *            用户实例
	 */
	public void saveOrUpdate(CategoryImg img, Users user) {
		this.log.info("保存或更新栏目域图片start");
		try {
			saveOrUpdateItem(img, user);

			this.log.debug("保存栏目域图片成功");
			this.log.info("保存或更新栏目域图片end");
		}
		catch (DataAccessException e) {
			this.log.error("[CategoryImg]saveOrUpdate发生错误！\n" + FormatExceptionStack.formatException(e));
		}

	}
}